package com.base.cn.platform.os.controller.web.course.comment;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.service.course.comment.CourseCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 课程评价
 *
 * @author SK
 * @since 2018-06-07
 */
@Controller
public class WebCourseCommentController extends WebBaseController {

    @Autowired
    private CourseCommentService courseCommentService;

    /**
     * 加载课程评价列表
     *
     * @param condition 课程评价查询条件
     * @return 课程评价列表
     */
    @RequestMapping("/web/course/comment/ajaxCourseCommentList")
    public String ajaxCourseCommentList(Model model,
                                        @RequestBody Map<String, Object> condition,
                                        @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage) {
        condition.put("status", 1);
        Map<String, Object> dataMap = courseCommentService.findCourseCommentPage(condition, currentPage, null, true, false, false);
        model.addAttribute("dataMap", dataMap);
        return resultPage("/course/cou/ajax-course-comment-list");
    }

    /**
     * 保存课程评价
     * @param comment 课程评价
     */
    @RequestMapping("/web/uc/course/comment/saveCourseComment")
    @ResponseBody
    public Map<String, Object> saveCourseComment(HttpServletRequest request,
                                                 @RequestBody Map<String, Object> comment) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        comment.put("userId", userId);
        comment.put("before",true);
        return courseCommentService.saveCourseComment(comment);
    }

    /**
     * 评价前验证
     * @param request HttpServletRequest
     * @param comment 评论数据对象
     * @return
     */
    @RequestMapping("/web/uc/course/comment/checkCourseCommentBefore")
    @ResponseBody
    public Map<String,Object> checkCourseCommentBefore(HttpServletRequest request,
                                                       @RequestBody Map<String,Object> comment){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        comment.put("userId", userId);
        comment.put("before",false);
        return courseCommentService.checkCourseCommentBefore(comment);
    }
}
